#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using Gizmox.WebGUI.Common.Resources;
using PLACEHR.Web.Categories.SystemSetting.Forms;
using PLACEHR.Web.Utilities;

#endregion

namespace PLACEHR.Web.Categories.SystemSetting.Controls
{
    public partial class GlobalSettingControl : UserControl, IHostedApplication
    {
        public GlobalSettingControl()
        {
            InitializeComponent();
            InitialData();
            Retrieve();
        }

        #region IHostedApplication

        public void InitializeApplication()
        {
        }

        public HostedToolBarElement[] GetToolBarElements()
        {
            ArrayList objElements = new ArrayList();
            objElements.Add(new HostedToolBarButton(LanLayer.GetTrans(20), LanLayer.GetTrans(20), new IconResourceHandle("Outlook.NewItem.gif"), "Retrieve"));
            objElements.Add(new HostedToolBarSeperator());
            objElements.Add(new HostedToolBarButton(LanLayer.GetTrans(21), LanLayer.GetTrans(21), new IconResourceHandle("CRM.ico_16_5006.gif"), "OpenSelected"));
            //objElements.Add(new HostedToolBarSeperator());
            //objElements.Add(new HostedToolBarButton("Add", "Add", new IconResourceHandle("CRM.ico_16_1070.gif"), "Add"));
            //objElements.Add(new HostedToolBarSeperator());
            //objElements.Add(new HostedToolBarButton("Delete", "Delete", new IconResourceHandle("CRM.ico_16_5010.gif"), "DeleteSelected"));

            return (HostedToolBarElement[])objElements.ToArray(typeof(HostedToolBarElement));
        }

        public void OnToolBarButtonClick(HostedToolBarButton objButton, EventArgs objEvent)
        {
            string strAction = (string)objButton.Tag;
            switch (strAction)
            {
                case "Retrieve":
                    Retrieve();
                    break;
                case "OpenSelected":
                    OpenDetailForm();
                    break;
                //case "Add":
                //    AddDetailForm();
                //    break;
                //case "DeleteSelected":
                //    if (lvwData.SelectedItems != null && lvwData.SelectedItems.Count > 0)
                //    {
                //        MessageBox.Show("Please make sure you want to delete selected Content!", "Prompt", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                //                        (obj, arg) =>
                //                        {
                //                            if (((MessageBoxWindow)obj).DialogResult == DialogResult.Yes)
                //                            {
                //                                DeleteSelected();
                //                                Retrieve();
                //                            }
                //                        });
                //    }
                //    else
                //    {
                //        MessageBox.Show("Please select which Content you want to delete!");
                //    }
                //    break;
            }
        }

        #endregion

        #region Method

        //private void DeleteSelected()
        //{
        //    foreach (ListViewItem lvwItem in lvwData.SelectedItems)
        //    {
        //        Content c = lvwItem.Tag as Content;
        //        var deleteContent = Global.Context.Contents.FirstOrDefault(p => p.ContentId == c.ContentId);

        //        List<ContentTranslation> deleteTransList = new List<ContentTranslation>();
        //        foreach (var trans in deleteContent.ContentTranslations)
        //        {
        //            var currTrans = Global.Context.ContentTranslations.FirstOrDefault(p => p.ContentTranslationId == trans.ContentTranslationId);
        //            deleteTransList.Add(currTrans);
        //        }

        //        foreach (var item in deleteTransList)
        //        {
        //            Global.Context.ContentTranslations.DeleteObject(item);
        //        }

        //        Global.Context.Contents.DeleteObject(deleteContent);
        //        LanLayer.RefreshItem(deleteContent.ContentId);
        //    }

        //    Global.Context.SaveChanges();
        //}

        private void OpenDetailForm()
        {
            if (lvwData.SelectedItem != null)
            {
                GlobalSetting item = lvwData.SelectedItem.Tag as GlobalSetting;
                GlobalSettingForm form = new GlobalSettingForm();
                form.currItem = item;
                form.FormClosed += new Gizmox.WebGUI.Forms.Form.FormClosedEventHandler(form_FormClosed);
                form.ShowDialog();
            }
        }

        void form_FormClosed(object sender, FormClosedEventArgs e)
        {
            GlobalSettingForm form = sender as GlobalSettingForm;
            if (form.DialogResult == DialogResult.OK)
            {
                Retrieve();
            }
        }

        private void AddDetailForm()
        {
            GlobalSettingForm form = new GlobalSettingForm();
            form.FormClosed += new Gizmox.WebGUI.Forms.Form.FormClosedEventHandler(form_FormClosed);
            form.ShowDialog();
        }

        private void InitialData()
        {
            this.Text = LanLayer.GetTrans(25);
            clhdSettingID.Text = LanLayer.GetTrans(7);
            clhdOffice.Text = LanLayer.GetTrans(6);
            clhdWorkingDaysPerMonth.Text = LanLayer.GetTrans(8);
            clhdWorkingHoursPerDay.Text = LanLayer.GetTrans(9);
            clhdPersonalTaxBase.Text = LanLayer.GetTrans(10);
            clhdAvgSalary.Text = LanLayer.GetTrans(11);
            clhdMinSalary.Text = LanLayer.GetTrans(12);
            //clhdMaxSocialBase.Text = LanLayer.GetTrans(13);
            //clhdMinSocialBase.Text = LanLayer.GetTrans(14);
            //clhdNonUrbanMaxSocialBase.Text = LanLayer.GetTrans(15);
            //clhdNonUrbanMinSocialBase.Text = LanLayer.GetTrans(121);
            //clhdMaxFundBase.Text = LanLayer.GetTrans(16);
            //clhdMinFundBase.Text = LanLayer.GetTrans(17);
            clhdUpdateDate.Text = LanLayer.GetTrans(26);
        }

        private void Retrieve()
        {

            try
            {
                List<GlobalSetting> list = Global.Context.GlobalSettings.ToList();

                lvwData.Items.Clear();
                lvwData.BeginUpdate();
                int No = 1;
                foreach (var item in list)
                {
                    ListViewItem lvwItem = new ListViewItem("");
                    lvwItem.SubItems.AddRange(new string[14]);

                    lvwItem.SubItems[0].Text = (No++).ToString();
                    lvwItem.SubItems[1].Text = item.ID.ToString();
                    lvwItem.SubItems[2].Text = !string.IsNullOrWhiteSpace(item.OfficeCode) ? item.Office.OfficeName : "ALL";
                    lvwItem.SubItems[3].Text = item.WorkingDaysPerMonth.HasValue ? item.WorkingDaysPerMonth.ToString() : string.Empty;
                    lvwItem.SubItems[4].Text = item.WorkingHoursPerDay.HasValue ? item.WorkingHoursPerDay.ToString() : string.Empty;
                    lvwItem.SubItems[5].Text = item.PersonalIncomeTaxBase.HasValue ? item.PersonalIncomeTaxBase.ToString() : string.Empty;
                    lvwItem.SubItems[6].Text = item.AverageSalary.HasValue ? item.AverageSalary.ToString() : string.Empty;
                    lvwItem.SubItems[7].Text = item.MinimumWage.HasValue ? item.MinimumWage.ToString() : string.Empty;
                    //lvwItem.SubItems[8].Text = item.MaxSocialInsuranceBase.HasValue ? item.MaxSocialInsuranceBase.ToString() : string.Empty;
                    //lvwItem.SubItems[9].Text = item.MinSocialInsuranceBase.HasValue ? item.MinSocialInsuranceBase.ToString() : string.Empty;
                    //lvwItem.SubItems[10].Text = item.NonUrbanMaxInsuranceBase.HasValue ? item.NonUrbanMaxInsuranceBase.ToString() : string.Empty;
                    //lvwItem.SubItems[11].Text = item.NonUrbanMinInsuranceBase.HasValue ? item.NonUrbanMinInsuranceBase.ToString() : string.Empty;
                    //lvwItem.SubItems[12].Text = item.MaxFundBase.HasValue ? item.MaxFundBase.ToString() : string.Empty;
                    //lvwItem.SubItems[13].Text = item.MinFundBase.HasValue ? item.MinFundBase.ToString() : string.Empty;
                    lvwItem.SubItems[14].Text = item.UpdateDate.ToString();
                    lvwItem.Tag = item;

                    lvwData.Items.Add(lvwItem);
                }
                lvwData.EndUpdate();
            }
            catch (Exception ex)
            { 
                
            }
        }

        #endregion

        #region Event

        private void lvwData_DoubleClick(object sender, EventArgs e)
        {
            OpenDetailForm();
        }

        #endregion
    }
}